<?php

if(STAINLESS != 1){
	require_once('../config.php');
	redirect("$home_url/admin/login");
}

$db = new models($host,$user,$pass,$db_name);
$id = isset($_POST['id']) ? $_POST['id'] : $_GET['id'];

if(!validate($id,DATA_NUMERIC)){
	redirect("$home_url/admin/products/?notice=invalid_id");
}

$db->where("id='$id'");
$product = $db->get_data("products");

if(!$product){
	redirect("$home_url/admin/products/?notice=update_error");
}

if(isset($_POST['request_edit'])){
	$params = array(
		'name'		=> trim($_POST['product_name']),
		'category'	=> $_POST['product_category'],
		'photo'		=> $_FILES['product_photo']['name'],
		'price'		=> trim($_POST['product_price']),
		'link'		=> trim($_POST['product_link'])
	);
	
	if(!validate($params['price'],DATA_NUMERIC)){
		$notice="only_numeric";
	}elseif($params['name'] && $params['category'] && $params['link']){
	
		$params['name'] = preg_replace("/[\/\'\"]/","",$params['name']);
		$db->where("name='".$params['name']."' AND id != '$id'");
		$pr_check = $db->get_data("products");
		
		if($pr_check){
			$error = "Sản phẩm đã có sẵn.";
		}else{
		
			if($_FILES['product_photo']['name']){
				$check = getimagesize($_FILES['product_photo']['tmp_name']);
				
				if($check !== false){
					if($check['mime'] == 'image/jpg' || $check['mime'] == 'image/jpeg' || $check['mime'] == 'image/png' || $check['mime'] == 'image/gif'){
						if($_FILES['product_photo']['size'] < 1048576){
							$path = HOME_DIR . "/images/photos/";
							$name_photo = unicode_convert(strtolower($params['name']));
							$name_photo = str_replace(" ","-",$name_photo) . "." . substr($check['mime'],6);
							$params['photo'] = $name_photo;
							$uploadOK = move_uploaded_file($_FILES['product_photo']['tmp_name'],$path.$name_photo);
							if(!$uploadOK){
								$error="Có lỗi xảy ra trong khi upload file, xin vui lòng thử lại sau nhé.";
							}
						}else{
							$error="Vui lòng chọn file hình ảnh nhỏ hơn 1MB nhé.";
						}
					}else{
						$error="Chỉ có thể upload file là hình ảnh thôi nhé.";
					}
				}else{
					$error="Chỉ có thể upload file là hình ảnh thôi nhé.";
				}
			}elseif($_POST['product_photo_url'] != ''){
				
				require_once(HOME_DIR . "/libraries/image.php");
				$source_url = $_POST['product_photo_url'];
				$name_photo = unicode_convert(strtolower($params['name'])) . ".jpg";
				$destination_url = HOME_DIR . "/images/photos/" . str_replace(" ","-",$name_photo);
				$resizeObj = new resize($source_url);
				$resizeObj -> resizeImage(200, 200, 'auto');//options: exact, portrait, landscape, auto, crop
				$resizeObj -> saveImage($destination_url, 100);
				$params['photo'] = $name_photo;
				
			}else{
				
				if($_POST['remove_image'] && $product[0]['photo']){
					$path_file = HOME_DIR . "/images/photos/" . $product[0]['photo'];
					unlink($path_file);
				}else{
					unset($params['photo']);
				}
			}
			
			$db->where("id='$id'");
			$db->update("products",$params);
			redirect("$home_url/admin/products/?notice=update_success");
		}
	}else{
		$error = "Không được bỏ trống dữ liệu cập nhật.";
	}
}
?>
<div class="container" id="page-product">
	<div class="row">
		<div class="col-sm-12 col-md-12 col-lg-12 text-center">
			<form role="form" class="form-horizontal" method="post" enctype="multipart/form-data" action="<?php echo $home_url; ?>/admin/products/edit">
				<input type="hidden" name="id" value="<?php echo $id;?>" />
				<fieldset class="margin20">
					<legend class="text-center padding10-bottom">CHỈNH SỬA SẢN PHẨM</legend>
					<?php
						if($error != ''){
							showAlert("alert-danger alert-error",$error);
						}
					?>
					<div class="form-group">
						<label for="product_link" class="control-label col-xs-3">Link sản phẩm</label>
						<div class="col-xs-6">
							<input type="text" name="product_link" id="product_link" class="form-control" size="40" readonly="true" value="<?php echo $product[0]['link']?>" placeholder="Nhập link sản phẩm..." />
						</div>
					</div>
					<div class="form-group">
						<label for="product_name" class="control-label col-xs-3">Tên sản phẩm</label>
						<div class="col-xs-6">
							<input type="text" name="product_name" id="product_name" class="form-control" value="<?php echo $product[0]['name']?>" size="40" placeholder="Nhập tên sản phẩm..." />
						</div>
					</div>
					<div class="form-group">
						<label for="product_price" class="control-label col-xs-3">Giá sản phẩm</label>
						<div class="col-xs-6">
							<input type="text" pattern="[0-9]+" name="product_price" id="product_price" class="form-control" value="<?php echo $product[0]['price']?>" size="40" placeholder="Nhập tên sản phẩm..." />
						</div>
					</div>
					<div class="form-group">
						<label for="product_category" class="control-label col-xs-3">Thuộc danh mục</label>
						<div class="col-xs-6">
							<input type="text" name="product_category" id="product_category" class="form-control" value="<?php echo $product[0]['category']?>" size="40" placeholder="Nhập danh mục sản phẩm..." />
						</div>
					</div>
					<div class="form-group">
						<label for="product_photo" class="control-label col-xs-3">Hình ảnh</label>
						<div class="col-xs-6">
							<input type="file" name="product_photo" class="form-control" style="padding:0" />
							<?php
								if($product[0]['photo']){
									echo "<img id='img_logo' width='100' src='$home_url/images/photos/".$product[0]['photo']."' />";
							?>
									<button data-toggle="tooltip" data-original-title="Xóa hình ảnh" id="request_remove_image" class="btn btn-danger"><i class="fa fa-trash fa-lg"></i></button>
									<input type="hidden" name="remove_image" id="remove_image" value="0" />
							<?php } ?>
						</div>
					</div>
					<div class="form-group">
						<div class="col-xs-offset-3 col-xs-6">
							<button type="submit" name="request_edit" id="request_edit" class="btn btn-primary">Cập nhật sản phẩm</button>
						</div>
					</div>
				</fieldset>
			</form>
		</div>
	</div>
</div>